package com.krlin.course.controller;


import com.krlin.course.entity.Course;
import com.krlin.course.entity.ListCourse;
import com.krlin.course.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
public class CourseController {

    @Autowired
    CourseService courseService;

    @RequestMapping(value = {"/", "/index", "/index.html"})
    public String index() {
        return "index";
    }

    @PostMapping("/getCollegeList")
    @ResponseBody
    List<String> getCollegeList() {
        return courseService.getCollegeList();
    }

    @PostMapping(value = "/getTeacherInfoList", produces = {"application/json;charset=utf-8"})
    @ResponseBody
    List<String> getTeacherInfoList(HttpServletRequest request) {
        String college = request.getParameter("data");
        return courseService.getTeacherInfoList(college);
    }

    @RequestMapping({"/search"})
    public String searchResult(HttpServletRequest request, Model model) {
        String teacherInfo = request.getParameter("teacherInfo");
        log.info("teacherInfo ========>" + teacherInfo);

        if (teacherInfo == null || "".equals(teacherInfo) || "--请选择--".equals(teacherInfo)) {
            return "redirect:/";
        }

        ListCourse lists = new ListCourse();
        lists.setCourses(courseService.getClassInfoList(teacherInfo));

        model.addAttribute("lists", lists);
        return "addinfo";
    }


    @PostMapping("/finish")
    public String finish(HttpServletRequest request) {
        String[] ids = request.getParameterValues("id");

        String[] onlines = request.getParameterValues("online");
        String[] ways = request.getParameterValues("way");
        String[] closes = request.getParameterValues("close");
        String[] bilis = request.getParameterValues("bili");

        String[] pingshis = request.getParameterValues("pingshi");
        String[] qimos = request.getParameterValues("qimo");
        String[] duojuans = request.getParameterValues("duojuan");
        String[] jiankaos = request.getParameterValues("jiankao");

        for (int i = 0;i < ids.length;i++) {
            Course course = new Course();

            course.setId(Integer.valueOf(ids[i]));

            course.setOnline(onlines[i]);
            course.setWay(ways[i]);
            course.setClose(closes[i]);
            course.setBili(bilis[i]);

            course.setPingshi(pingshis[i]);
            course.setQimo(qimos[i]);
            course.setDuojuan(duojuans[i]);
            course.setJiankao(jiankaos[i]);

            courseService.save(course);
        }
        return "redirect:/success";
    }
}
